<?php

namespace Modules\Production\Repository;

use App\Services\InventoryService;
use Illuminate\Support\Facades\DB;
use Modules\Base\Repository\BoxNumberRepository;
use Modules\Production\Models\DassemblyOrderProduct;
use App\Enums\InvTransDocType;
use App\Enums\DassemblyProductStatus;

/**
 * 组装拆卸成品相关
 * Class DassemblyOrderProductRepository
 * @package Modules\Production\Repository
 */
class DassemblyOrderProductRepository
{
    /**
     * 根据id查询成品信息
     * @param $id
     * @return mixed
     */
    static public function getProductById($id){
        return DassemblyOrderProduct::with(['material','order'])->find($id);
    }

    /**
     * 根据id修改成品信息
     * @param $id
     * @param $data
     * @return mixed
     */
    static public function updateProductById($id,$data){
        return DassemblyOrderProduct::where('id',$id)->update($data);
    }

    /**
     * 拆卸成品出库扫描
     * @param $id
     * @param $lotnum
     */
    static public function DassemblyOutScan($productId,$lotnum){

        //成品行查询
        $product = DassemblyOrderProductRepository::getProductById($productId);
        if(!$product){
            throw new \Exception('成品id不存在');
        }

        //库存查询
        $inv = InventoryService::getInventoryByLotnum($lotnum,$product->cargo_code,$product->order->org_code)->toArray();
        if(empty($inv)){
            throw new \Exception('该箱号无库存信息');
        }

        $invList = [];
        foreach ($inv as &$item){

            $item['release_qty'] = $item['qty'] - $item['assign_qty'] - $item['lock_qty'];

            if($item['material_no'] != $product->material->fnumber || $item['release_qty'] <= 0 || $item['wms_code'] != $product->wms_code){
                continue;
            }
            $invList[] = $item;
        }

        if(empty($invList)){
            throw new \Exception('当前箱号不存在符合出库的产品');
        }

        return $invList;

    }

    /**
     * 拆卸成品出库提交
     * @param $id
     * @param $postData
     */
    static public function DassemblyOut($productId,$postData){

        if(empty($postData)){
            throw new \Exception('提交数据不能为空');
        }

        $product = self::getProductById($productId);
        if(!$product){
            throw new \Exception('成品id不存在');
        }

        if($product->status != DassemblyProductStatus::WAIT->value){
            throw new \Exception('成品已出库，请勿重复操作！');
        }

        $totalQty = array_sum(array_column($postData,'qty'));
        if($totalQty != $product->qty){
            throw new \Exception('提交总数量必须与成品数量一致');
        }

        try{
            DB::beginTransaction();
            foreach ($postData as $item){
                //出库
                InventoryService::decInventory(
                    $product->order->org_code,
                    $product->order->docno,
                    $product->order->id,
                    InvTransDocType::DASSEMBLY_ASSEMBLY_ORDER->value,
                    $product->material->fnumber,
                    $item['location'],
                    $item['lotnum'],
                    $item['qty'],
                    $product->cargo_code
                );
            }

            self::updateProductById($productId,['status' => DassemblyProductStatus::OUT->value]);

            DB::commit();
            return true;
        }catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }
    }

    /**
     * 拆卸子件入库扫描
     */
    static public function DassemblyDetailInScan($productId,$lotnum){

        $lotInfo = BoxNumberRepository::getBoxByCode($lotnum);
        if(!$lotInfo){
            throw new \Exception('箱号不存在');
        }

        return [];
    }

    /**
     * 拆卸子件入库提交
     * @param $productId
     * @param $postData
     */
    static public function DassemblyDetailIn($productId,$postData){

        foreach ($postData as $row){



        }

    }

}
